package com.telit.uav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.telit.common.exception.ForestBaseException;
import com.telit.uav.dao.CommAirwayDao;
import com.telit.uav.entity.CommAirway;
import com.telit.uav.service.AirwayService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * @Author:zhangwei
 * @Description:
 * @Time:Created on 2021/10/16 9:08
 */
@Service
public class AirwayServiceImpl extends ServiceImpl<CommAirwayDao, CommAirway> implements AirwayService {
    /**
     * 新增航线信息
     */
    @Override
    public void add(CommAirway airway) {
        if(baseMapper.insert(airway)<=0){
            throw new ForestBaseException("新增航线信息失败");
        }
    }

    /**
     * 修改航线信息
     * @param airway
     */
    @Override
    public void modify(CommAirway airway) {
        if(baseMapper.updateById(airway)<=0){
            throw new ForestBaseException("修改航线信息失败");
        }
    }

    /**
     * 分页条件航线信息
     * @param current
     * @param size
     * @param airway
     * @return
     */
    @Override
    public IPage<CommAirway> selectByPage(Integer current, Integer size, CommAirway airway) {
        IPage<CommAirway> airwayIPage=new Page<>(current, size);
        return baseMapper.selectPage(airwayIPage,getWrapper(airway));
    }

    public QueryWrapper<CommAirway> getWrapper(CommAirway airway){
        if(airway==null){
            return null;
        }
        QueryWrapper<CommAirway > wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(airway.getAirwayType())){
            wrapper.eq("airway_type",airway.getAirwayType());
        }
        if (!StringUtils.isEmpty(airway.getAirwayName())){
            wrapper.like("airway_name",airway.getAirwayName());
        }
        wrapper.orderByDesc("update_time");
        return wrapper;
    }


    /**
     * 无分页条件查询航线信息
     * @param airway
     * @return
     */
    @Override
    public List<CommAirway> selectByNoPage(CommAirway airway) {
        return baseMapper.selectList(getWrapper(airway));
    }

    /**
     * 根据ID删除航线信息
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        if(baseMapper.deleteById(id)<=0){
            throw new ForestBaseException("删除航线信息失败");
        }
    }

    /**
     * 根据id查询航线信息
     * @return
     */
    @Override
    public CommAirway queryById(Long id) {
        return baseMapper.selectById(id);
    }


}
